package edu.cs.usask.ca.silver.plugin.rcp.handlers.create;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.handlers.HandlerUtil;

import edu.cs.usask.ca.silver.businesslogic.ids.GroupID;
import edu.cs.usask.ca.silver.plugin.rcp.Activator;
import edu.cs.usask.ca.silver.plugin.rcp.dialogs.create.CreateModelProjectDialog;
import edu.cs.usask.ca.silver.plugin.rcp.handlers.create.helper.CreateDataProcedures;
import edu.cs.usask.ca.silver.plugin.rcp.views.projectviewer.ProjectViewI;

public class CreateModelProjectHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {

		ProjectViewI view = (ProjectViewI) HandlerUtil
				.getActiveWorkbenchWindow(event).getActivePage()
				.findView(event.getParameter("projectviewid"));

		CreateModelProjectDialog dialog;
		GroupID selectedGroupId = (GroupID) view
				.getTreeSelectionAsSilverTreeObject().getSilverObjectId();
		dialog = new CreateModelProjectDialog(Activator.getDefault()
				.getWorkbench().getActiveWorkbenchWindow().getShell(),
				selectedGroupId);
		dialog.create();
		if (dialog.open() == Window.OK) {
			CreateDataProcedures
					.createNewMpFromCreateMpDialogWithAGroupIdSelected(dialog,
							view.getAssociatedStorage());
			view.updateTree();
		}

		return null;
	}

}
